package com.topisv.tms.repository.stock.instorage;

import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.topisv.tms.entity.stock.instorage.InstorageCountTask;
import com.topisv.tms.entity.stock.instorage.pre.InstorageBillItemPre;
import com.topisv.tms.entity.stock.instorage.pre.InstorageBillPre;

/**
 * 入库点收任务_dao
 * @author ch
 *
 */
public interface InstorageCountTaskDao extends PagingAndSortingRepository<InstorageCountTask, Long>, JpaSpecificationExecutor<InstorageCountTask> {

	@Query("select t from InstorageCountTask t where t.taskCode=?")
	InstorageCountTask loadByTaskCode(String taskCode);

	@Query("select t from InstorageCountTask t where t.billCode=?")
	InstorageCountTask loadByBillCode(String billCode);
	
	@Query("select p from  InstorageBill b , InstorageBillPre p   where  b.aboutCode = p.billCode  and b.billCode = ? ")
	InstorageBillPre findInstorageBillPreExist(String taskCode);
	
	@Query("select ip from  InstorageBillPre p  , InstorageBillItemPre ip where ip.billPreId = p.id and p.billCode = ?1 and ip.skuCode = ?2 ")
    List<InstorageBillItemPre> findSkuExist(String billCode,String skuCode);
	
	@Query("select ip from  InstorageBillPre p  , InstorageBillItemPre ip where ip.billPreId = p.id and p.billCode = ?1 and ip.skuCode = ?2 "
	    + " and (ip.batchCode = ?3 or ip.batchCode = '' or ip.batchCode is null) and "
	    + "(ip.productDate = ?4 or ip.productDate is null or ip.productDate = '') and "
	    + "(ip.expireDate = ?5 or ip.expireDate is null or ip.expireDate = '')")
	List<InstorageBillItemPre> findSkuExistNew(String billCode,String skuCode, String batchCode, String productDate, String expireDate);

}